package com.wiz.web.controllor;

import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.common.ResponseCode;
import com.wiz.service.CardTockService;
import com.wiz.to.CardTockTO;
import com.wiz.util.ExcelUtil;
import com.wiz.vo.CollectionVO;
import com.wiz.vo.ResponseJsonBody;
import com.wiz.vo.criteria.CardTockSearchCriteriaTO;

/**
 * 卡片信息
 * @author xxy
 *
 */
@Controller
@RequestMapping("/cardTock")
public class CardTockControllor implements ResponseCode {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	
	@Resource
	private CardTockService cardTockService;
	
	
	@RequestMapping(value="/toAllotHq")
	public String toAllotHq(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model){
		
		return "cardTock/allotHq";
		
	}
	@RequestMapping(value="/toExportCard")
	public String toExportCard(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model){
		
		return "cardTock/exportCard";
		
	}
	@RequestMapping(value="/allotHq")
	@ResponseBody
	public String allotHq(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "file", required = true) MultipartFile file,
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		InputStream in = null;;
		try {
			in = file.getInputStream();
			List<List<String>>dataList = ExcelUtil.read(in);
			
			cardTockService.addbatch(dataList);
			json = new ResponseJsonBody("200", "导入成功", "cardTock",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "cardTock",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
		
	}
	@RequestMapping(value="/exportCard")
	@ResponseBody
	public String exportCard(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "file", required = true) MultipartFile file,
			HttpSession session, Model model){
		ResponseJsonBody json = null;
		InputStream in = null;;
		try {
			in = file.getInputStream();
			List<List<String>>dataList = ExcelUtil.read(in);
			cardTockService.batchUpdate(dataList);
			json = new ResponseJsonBody("200", "导入成功", "cardTock",
					"navTab", "closeCurrent", null, null);
		} catch (Exception e) {
			e.printStackTrace();
			json = new ResponseJsonBody("300", e.getMessage(), "cardTock",
					"navTab", "closeCurrent", null, null);
		}
		return gson.toJson(json);
		
	}
	@RequestMapping(value="/list")
	public String getCardTocks(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "numPerPage", required = false,defaultValue="10") int pageSize,
			@RequestParam(value = "pageNum", required = false,defaultValue="1") int pageNo,
			@RequestParam(value = "hqId", required = false) String hqId,
			@RequestParam(value = "hqName", required = false) String hqName,
			@RequestParam(value = "stationId", required = false) String stationId,
			@RequestParam(value = "stationName", required = false) String stationName,
			@RequestParam(value = "cardNo", required = false) String cardNo,
			@RequestParam(value = "phyCardNo", required = false) String phyCardNo,
			@RequestParam(value = "cardType", required = false) Integer cardType,
			HttpSession session, Model model){
		CardTockSearchCriteriaTO searchCriteria = new CardTockSearchCriteriaTO(hqId, hqName, stationId,
				stationName, cardNo, phyCardNo, cardType, pageSize, pageNo, (pageNo > 0 && pageSize > 0));
		
		List<CardTockTO> agentTOs=cardTockService.getCardTocks(searchCriteria);
		int totalCount=cardTockService.getCardTockCnt(searchCriteria);
		CollectionVO collectionTO=new CollectionVO(agentTOs, totalCount, pageSize, pageNo);
		model.addAttribute("collectionTO", collectionTO);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNumShow", totalCount/pageSize+((totalCount%pageSize>0)?1:0));
		model.addAttribute("searchCriteria", searchCriteria);
		return "cardTock/list";
		
	}
	@RequestMapping(value="/add")
	@ResponseBody
	public String create(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "hqId", required = true) String hqId,
			@RequestParam(value = "hqName", required = true) String hqName,
			@RequestParam(value = "stationId", required = true) String stationId,
			@RequestParam(value = "stationName", required = true) String stationName,
			@RequestParam(value = "cardNo", required = true) String cardNo,
			@RequestParam(value = "phyCardNo", required = true) String phyCardNo,
			@RequestParam(value = "verificationCode", required = true) String verificationCode,
			@RequestParam(value = "cardType", required = true) int cardType,
			HttpSession session, Model model){
		
		return DATA_OK;
		
	}
	
}
